package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Comments;
import org.csu.secondhand.api.entity.Posting;
import org.csu.secondhand.api.persistence.CommentsMapper;
import org.csu.secondhand.api.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("commentsService")
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    CommentsMapper commentsMapper;

    @Override
    public Comments insertComments(Comments comments) {
        commentsMapper.insert(comments);
        return comments;
    }

    @Override
    public Comments deleteComments(long id) {
        Comments comments = commentsMapper.selectById(id);
        commentsMapper.deleteById(id);
        return comments;
    }

    @Override
    public List<Comments> getCommentsByPosting(long posting) {
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("posting", posting);
        return commentsMapper.selectList(queryWrapper);
    }

    @Override
    public List<Comments> getCommentsByUser(String user) {
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user);
        return commentsMapper.selectList(queryWrapper);
    }

    @Override
    public Comments getCommentsById(long id) {
        return commentsMapper.selectById(id);
    }



}
